package com.finalist.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.finalist.dao.WinPeopleInfoDao;
import com.finalist.entity.WinPeopleInfo;
import com.finalist.service.WinPeopleInfoService;

@Service("winPeopleInfoServiceImpl")
public class WinPeopleInfoServiceImpl extends BaseServiceImpl<WinPeopleInfo, Long> implements WinPeopleInfoService {

	@Resource(name = "winPeopleInfoDaoImpl")
	private WinPeopleInfoDao winPeopleInfoDao;
	
	//@Resource(name = "winPeopleInfoDaoImpl")
	public void saveWinPeopleInfo(WinPeopleInfo winPeopleInfo) {
		super.save(winPeopleInfo);
	}
	
	@Resource(name = "winPeopleInfoDaoImpl")
	public void setBaseDao(WinPeopleInfoDao winPeopleInfoDao) {
		super.setBaseDao(winPeopleInfoDao);
	}

	@Override
	public WinPeopleInfo findByToken(String token) {
		// TODO Auto-generated method stub
		return winPeopleInfoDao.findByToken(token);
	}

	@Override
	public void releaseStock() {
		// TODO Auto-generated method stub
		List<WinPeopleInfo> list = winPeopleInfoDao.findByNotSubmit();
		for (int i = 0; i < list.size(); i++) {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date d1;
			Date d2;
			
			Date now = new Date(); 
			
			try {
				d1 = df.parse( df.format( now ));
				d2 = df.parse(df.format( list.get(i).getCreateDate() ));
				 long diff = d1.getTime() - d2.getTime();
				 if (diff > 1000 * 60 * 30) {
					 this.delete(list.get(i));
				 }
				
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
	}
	
}
